package com.tgy.binarysearch;

import java.util.Objects;

/**
 * @Author: tgy
 * @Date: 2020-11-17 17:06
 *  二分查找返回刚好小于key的元素下标，如无return -1
 */
public class BSFirstLTValue {

    public int binarySearch(Integer[] nums, int value) {


        if (Objects.isNull(nums) || nums.length == 0) {

            return -1;
        }

        int left = 0,right = nums.length,mid;

        while (left < right) {

            mid = (left + right) >> 1;

            if (value <= nums[mid]) {

                right = mid;
            }else {

                left = mid + 1;
            }
        }


        return left > 0 ? left - 1: -1;
    }
}
